package com.relife.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.ClientManager;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.core.easyui.Easyui;
import com.relife.core.easyui.Tree;
import com.relife.core.entity.CodeEntity;
import com.relife.core.qbc.QbcQuery;
import com.relife.sys.service.CodeService;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("sys/code")
public class CodeController extends BaseController {
	@Autowired
	private CodeService codeService;

	/**
	 * 页面
	 * 
	 * @author WU
	 * @date 2015-5-20 下午7:54:20
	 * @return
	 */
	@RequestMapping("/codeList")
	public String codeList() {
		return "sys/code/codeList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2015-5-20 下午7:54:08
	 * @param page
	 */
	@RequestMapping("/listData")
	public void listData(CodeEntity code, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(code);
		qbc.add(Restrictions.isNull("parent"));
		codeService.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年4月3日 下午2:18:43
	 * @return
	 */
	@RequestMapping("/codeChildrenList")
	public String codeChildrenList() {
		return "sys/code/codeChildrenList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2015-5-20 下午7:54:08
	 * @param page
	 */
	@RequestMapping("/childrenData")
	public void childrenData(CodeEntity code, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(code);
		codeService.getPage(qbc, page);
		Easyui.datagrid(page, res);
	}

	/**
	 * 
	 * @author WU
	 * @date 2015年11月26日 下午12:44:24
	 * @param code
	 * @param req
	 * @return
	 */
	@RequestMapping("/code")
	public String code(CodeEntity code, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(code.getId())) {
			code = codeService.get(CodeEntity.class, code.getId());
		}
		req.setAttribute("page", code);
		return "sys/code/code";
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年4月3日 下午2:20:43
	 * @param code
	 * @param req
	 * @return
	 */
	@RequestMapping("/codeChildren")
	public String codeChildren(CodeEntity code, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(code.getId())) {
			code = codeService.get(CodeEntity.class, code.getId());
		}
		req.setAttribute("page", code);
		return "sys/code/codeChildren";
	}

	@ResponseBody
	@RequestMapping("/save")
	public AjaxJson save(CodeEntity code) {
		AjaxJson j = new AjaxJson();
		try {
			codeService.saveCode(code);
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}

	@ResponseBody
	@RequestMapping("/del")
	public AjaxJson del(CodeEntity code) {
		AjaxJson j = new AjaxJson();
		try {
			codeService.delete(code);
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}

	/**
	 * 
	 * @author WU
	 * @date 2015年12月1日 下午3:01:28
	 * @param res
	 */
	@RequestMapping("/tree")
	public void tree(Long id, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(CodeEntity.class);
		if (id == null) {
			qbc.add(Restrictions.isNull("parent"));
		} else {
			qbc.add(Restrictions.eq("parent.id", id));
		}
		qbc.addOrder(Order.asc("orderBy"));
		List<CodeEntity> list = codeService.getList(qbc);
		Tree tree = new Tree("id", "name", "children");
		Easyui.tree(list, tree, res);
	}

	/**
	 * 刷新数据字典
	 * 
	 * @author WU
	 * @date 2015年9月8日 下午7:54:27
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/reload")
	public AjaxJson reload(CodeEntity code) {
		AjaxJson j = new AjaxJson();
		try {
			String hql = "select distinct c from CodeEntity as c left join fetch c.children where c.parent is null";
			List<CodeEntity> codes = codeService.getListByHql(hql);
			ClientManager.getInstance().clearCode();
			ClientManager.getInstance().addCode(codes);
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}
}
